package com.cc8w.admins.controller;


import com.alibaba.fastjson.JSONObject;
import com.cc8w.admins.service.ProductClassService;
import com.cc8w.entity.CategoryEntity;
import com.cc8w.entity.GoodsEntity;
import com.cc8w.entity.ProductAttributeEntity;
import com.cc8w.util.PageData;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

@Controller
@RequestMapping("/admin/goods")
@Validated
public class GoodsController extends AdminBaseController {
    @Autowired
    private ProductClassService productClassService;

    @RequestMapping("addgoods")
    public String addGoods(){
        //1.获得模型分类
        List<PageData> typeList = productClassService.queryTypeList();
        this.model.addAttribute("productTypeList", typeList);
        //2.获取产品分类
        List<CategoryEntity> catList = productClassService.getGoodsCat();
        this.model.addAttribute("goodsCatList", catList);

        return "/admin/goods/addgoods";
    }

    @RequestMapping("insertgoods")
    @ResponseBody()
    public PageData insertGoods(@RequestBody Map<String, Object> goods_info){
        System.out.println(goods_info);
        //1.产品先入库
        int modId = Integer.parseInt((String)goods_info.get("modId")) ;
        int catId = Integer.parseInt((String)goods_info.get("goodsCat")) ;
        String goodsName = (String)goods_info.get("goodsName") ;
        String goodsNo = (String)goods_info.get("goodsNo") ;
        BigDecimal price = new BigDecimal((String)goods_info.get("price"))  ;
        BigDecimal  originalPrice = new BigDecimal((String)goods_info.get("originalPrice")) ;
        String goodsImg = (String)goods_info.get("goodsImg") ;
        String goodsDesc = (String)goods_info.get("goodsDesc") ;
        String goodsDescImg = (String)goods_info.get("goodsDescImg") ;
        //int sort = Integer.parseInt((String)goods_info.get("sort")) ;

        //组装数据
        GoodsEntity goodsEntity = new GoodsEntity();
        goodsEntity.setModId(modId);
        goodsEntity.setCatId(catId);
        goodsEntity.setGoodsName(goodsName);
        goodsEntity.setGoodsNo(goodsNo);
        goodsEntity.setPrice(price);
        goodsEntity.setOriginalPrice(originalPrice);
        goodsEntity.setGoodsImg(goodsImg);
        goodsEntity.setGoodsDesc(goodsDesc);
        goodsEntity.setGoodsDescImg(goodsDescImg);
        goodsEntity.setCreateTiem(new Date());
        goodsEntity.setSort(1);

        //执行sql
        productClassService.insertGoods(goodsEntity);
        System.out.println(goodsEntity.getId());
        int goodsId = goodsEntity.getId();

        ArrayList< HashMap<String,Object> > al = (ArrayList<HashMap<String, Object>>) goods_info.get("goods_prices");

        //2.价格要循环的入库(注意：先查询数据库是否存在,更新或新增[一般情况下都是新增])

        for (HashMap<String,Object> a :al){
            System.out.println(a);
            String goodsSn = (String) a.get("sn");
            Float goodsPrice = Float.parseFloat((String)a.get("price"));
            ArrayList<HashMap<String,Object>> values = (ArrayList<HashMap<String,Object>>) a.get("values");
            String priceValues = JSONObject.toJSONString(values);
            productClassService.insertGoodsPrice(goodsId,goodsSn,goodsPrice,priceValues);

            //3.把values数组中的值写入商品属性表goods_attr(主要记录产品id,属性id,值id)
            for (HashMap<String,Object> value : values){
                System.out.println(value);
                int attrId = (int) value.get("attr_id");
                int valId = (int) value.get("id");
                String attrValue = (String)value.get("attr_value") ;
                productClassService.insertGoodsAttr(goodsId,attrId,valId,attrValue);
            }

        }


        PageData res = new PageData();
        res.put("code",0);
        res.put("msg","success");
        res.put("data","");
        return res;
        //return "/admin/goods/insertgoods";
    }
    /**
     * 根据模型id获得属性列表
     */
    @RequestMapping("getattrlist")
    @ResponseBody()
    public PageData getProductAttrList(@RequestParam(value="mod_id",defaultValue = "") int mod_id){
        PageData pageData = this.getPageData();
        //1.获得模型属性列表(只获取前10条属性)
        PageHelper.startPage(1,50);
        List<ProductAttributeEntity> typeList = productClassService.getProductAttributeList(pageData);
        System.out.println(typeList);


        PageData res = new PageData();
        res.put("code",0);
        res.put("msg","success");
        res.put("data",typeList);
        return res;
    }

    /**
     * 商品分页
     */
    @RequestMapping("goodslist")
    @ResponseBody()
    public String goodslist(@RequestParam(value="currentPage",defaultValue = "1") int currentPage,
                            @RequestParam(value="showCount",defaultValue = "5") int showCount,
                            @Size(min=6,max=8,message = "排序大小不合适") @PathVariable("sort") @RequestParam(defaultValue = "add_time") String sort,
                            @RequestParam(defaultValue = "desc") String order){
        //1.获得页数和页大小
        //2.是否有搜索或排序
        System.out.println(sort);


        //return "/admin/goods/goodslist";
        return null;
    }










}
